#include <iostream>

using namespace std;
/*
	虚继承的构造方法：
		1、只会被调用一次 -- 在最后的子类中被调用
		2、虚继承的构造方法，优先于 非虚继承的构造方法被调用
*/
class A
{
public:
	A(const char *s) { 
		cout << "Class A:" << s << endl; 
	}
	~A() {}
};

class B: virtual public A
{
public:
	B(const char *s1, const char *s2): A(s1)
	{ cout<<"Class B:"<<s2<<endl; }
};

class C: virtual public A {
public:
	C(const char *s1, const char *s2): A(s1)
	{ cout<<"Class C:"<<s2<<endl; }
};

class D: public B, public C {
public:
	// 对A进行虚继承，A的构造方法只会被最后的子类D调用 -- B、C不再调用
	D(const char *s1, const char *s2, const char *s3, const char *s4)
	: C(s2,s3), B(s4,s2), A(s1)
	{ cout<<"Class D:"<<s4<<endl; }
};


int main(int argc, char *argv[]) {
	D *ptr = new D("str1", "str2", "str3", "str4");
	delete ptr;
	/*
		Output:
			Class A: str1
			Class B: str2
			Class C: str3
			Class D: str4
	*/
	
	/*
	If no virtuals (no A(s1) in D)
		Output:
			Class A: str4
			Class B: str2
			Class A: str2
			Class C: str3
			Class D: str4
	
	*/
}